{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1d3e4889-a4a0-4167-82cc-086698ca9a4e",
   "metadata": {},
   "source": [
    "##### ❇️ Pandas 🐼: df.apply(func, axis)\n",
    "Objects passed to the function are Series objects whose index is either <br> the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b034ae9d-d565-4ca1-bfb9-67079b76786c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e69b9a5c-a4b5-45c8-a3b5-f04d73449df5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B\n",
       "0  1  4\n",
       "1  2  5"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'A': [1, 2], 'B': [4, 5]})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fee22636-3ab1-404d-99b0-1041f83d2f9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    3\n",
       "B    9\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# When axis = 0, every dataframe column is passed as a series to func\n",
    "df.apply(func = np.sum, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a18e0851-9f47-4436-aeef-4acf9d07574c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    5\n",
       "1    7\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# When axis = 1, every dataframe row is passed as a series to func\n",
    "df.apply(func = np.sum, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4fb4dacb-5351-4095-9715-4440913de146",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>A_plus_B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B  A_plus_B\n",
       "0  1  4         5\n",
       "1  2  5         7"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# using a custom lambda function; axis = 1\n",
    "# Notice axis = 1, which means each row is passed as a series whose index \n",
    "# is data frame's column, that's why we are able to access values row['A'], row['B'] etc.\n",
    "A_plus_B = df.apply(func = lambda row: row['A'] + row['B'], axis=1)\n",
    "df['A_plus_B'] = A_plus_B\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ca087042-d8f4-4ea8-9eea-5c722804956d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A           1.5\n",
       "B           4.5\n",
       "A_plus_B    6.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# calculating average of each column\n",
    "# Notice axis = 0, which means each column is passed as a series whose index \n",
    "# is data frame's index, that's why we are able to access values col[0], col[1] etc.\n",
    "df.apply(func = lambda col: (col[0] + col[1])/2, axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27ea53dc-39e9-40b5-9567-e2796f466bab",
   "metadata": {},
   "source": [
    "##### ❇️ Hope you enjoyed reading!! 📖 \n",
    "##### ❇️ follow → @akshay_pachaar  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b87a73e5-a4e5-4d01-8df5-b1fcc888b1d4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
